package io.jft.doll.weidget

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.g.base.extend.dp

/**
 * Created by G on 2017/12/20 0020.
 */
class AutoFixRoomHeightViewGroup : LinearLayout {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private val rect = Rect()

    init {
        getWindowVisibleDisplayFrame(rect)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        var viewHeight = 0

        if (getChildAt(0).visibility != View.GONE) {
            measureChild(getChildAt(0), widthMeasureSpec, MeasureSpec.makeMeasureSpec(rect.bottom - rect.top - 24.dp(), MeasureSpec.EXACTLY))
            viewHeight += getChildAt(0).measuredHeight
        }
        if (getChildAt(1).visibility != View.GONE) {
            measureChild(getChildAt(1), widthMeasureSpec, MeasureSpec.makeMeasureSpec(rect.bottom - rect.top - 8.dp(), MeasureSpec.EXACTLY))
            viewHeight += getChildAt(1).measuredHeight
        }


        setMeasuredDimension(View.MeasureSpec.getSize(widthMeasureSpec), viewHeight)
    }

}